package com.lc.qianrenshiye.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.lc.qianrenshiye.BaseActivity;
import com.lc.qianrenshiye.BaseApplication;
import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.adapter.HomeShowAdapter;
import com.lc.qianrenshiye.adapter.HomeTabAdapter;
import com.lc.qianrenshiye.adapter.OrderAdapter;
import com.lc.qianrenshiye.adapter.ShopCarAdapter;
import com.lc.qianrenshiye.conn.ClearnShopCarPost;
import com.lc.qianrenshiye.conn.GoodsDetailGet;
import com.lc.qianrenshiye.conn.GoodsListGet;
import com.lc.qianrenshiye.conn.GoodsTypeGet;
import com.lc.qianrenshiye.conn.ShopCarShowGet;
import com.lc.qianrenshiye.conn.SubmitOrderPost;
import com.lc.qianrenshiye.conn.UpdateShopCarPost;
import com.lc.qianrenshiye.dialog.NotPayDialog;
import com.lc.qianrenshiye.uitls.MyAnimationUitls;
import com.lc.qianrenshiye.uitls.MyToast;
import com.lc.qianrenshiye.uitls.SetTagUtils;
import com.lc.qianrenshiye.widget.HorizontalListView;
import com.lc.qianrenshiye.widget.MyIndicator;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/15.
 */
public class HomeActivity extends BaseActivity implements View.OnClickListener, ShopCarAdapter.AddOrMinusListener, View.OnTouchListener, GestureDetector.OnGestureListener, OrderAdapter.PayOrContinue, MyIndicator.MyIndicatorOnclickListener {

    @BoundView(R.id.convenientBanner) private ConvenientBanner convenientBanner;
    @BoundView(R.id.home_tab_lv) private HorizontalListView home_tab_lv;
    @BoundView(R.id.home_indicator) private MyIndicator home_indicator;
    @BoundView(R.id.home_horizontal_sv) private HorizontalScrollView home_horizontal_sv;
    @BoundView(R.id.home_show_lv) private ListView home_show_lv;
    @BoundView(R.id.home_list_no_show_tv) private TextView home_list_no_show_tv;//没有时候显示
    @BoundView(R.id.home_tab_iv) private ImageView home_tab_iv;//点击 listview 向右移动
    @BoundView(R.id.home_search_iv) private ImageView home_search_iv;//点击这个显示 home_search_search_iv
    @BoundView(R.id.home_search_search_iv) private ImageView home_search_search_iv;
    @BoundView(R.id.home_search_click_gone_iv) private ImageView home_search_click_gone_iv;
    @BoundView(R.id.home_search_gone_rl) private RelativeLayout home_search_gone_rl;
    @BoundView(R.id.home_search_content_et) private EditText home_search_content_et;
    @BoundView(R.id.home_business_center_iv) private ImageView home_business_center_iv;
    @BoundView(R.id.home_shop_car_iv) private ImageView home_shop_car_iv;
    @BoundView(R.id.home_shopcar_add_show_tv) private TextView home_shopcar_add_show_tv;
    @BoundView(R.id.home_ll) private LinearLayout home_ll;

    @BoundView(R.id.home_right_title_tv) private TextView home_right_title_tv;
    @BoundView(R.id.home_right_price_tv) private TextView home_right_price_tv;
    @BoundView(R.id.home_right_title_two_tv) private TextView home_right_title_two_tv;
    @BoundView(R.id.home_right_content_tv) private TextView home_right_content_tv;
    @BoundView(R.id.home_right_bottom_title_tv) private TextView home_right_bottom_title_tv;
    @BoundView(R.id.home_add_to_shopcar_tv) private TextView home_add_to_shopcar_tv;
    @BoundView(R.id.home_right_content_ll) private LinearLayout home_right_content_ll;
    @BoundView(R.id.home_right_scroll_sv) private ScrollView home_right_scroll_sv;
    private TextView item_shop_table_num_tv;

    private HomeTabAdapter homeTabAdapter;
    private HomeShowAdapter homeShowAdapter;
    private ShopCarAdapter shopCarAdapter;
    private List<GoodsTypeGet.GoodsType> tabLists = new ArrayList<>();
    private int tabSelPosition;
    private int showSelPosition;
    private List<ShopCarShowGet.ShopCarEntity> shopCarLists = new ArrayList<>();
    private List<GoodsListGet.GoodsEntity> goodsLists = new ArrayList<>();

    private GoodsDetailGet.GoodsDetailEntity goodsDetail;

    private PopupWindow mPopupWindow;
    private GestureDetector detector;

    private boolean isClick = true; // 判断购物车加减点击是否有效
    private boolean isFromOrder; //判断是否是订单过来的  判断继续加单时候用不用座位号
    private String orderId = ""; //订单号 加菜时候用 默认值是空的
    private NotPayDialog notPayDialog;

    private int addOrMinusPos; //加或减的item的位置  购物车修改接口 成功时候用

    private boolean isAdd;  // 判断购物车是否被添加过东西 和 shopcarlist的长度一起判断是否清空购物车


    private ClearnShopCarPost clearnShopCarPost = new ClearnShopCarPost(new AsyCallBack<String>() {

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

//            MyToast.showToast(toast, context);

            shopCarLists.clear();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

//            MyToast.showToast(toast, context);

            Log.e("cleanShopCar---onFail", toast);

        }
    });

    //下单
    private SubmitOrderPost submitOrderPost = new SubmitOrderPost(new AsyCallBack<String>() {
        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {


            shopCarLists.clear();

            isAdd = false;//清空这两个 是让清空购物车方法 里面不走 清空接口，只把别的数据清空，因为下单成功了，购物车就没东西了

            cleanShopCar();

            switch (type) {

                case 0:

                    startActivity(new Intent(HomeActivity.this, OrderActivity.class));

                    break;

                case 1:

                    Bundle bundle = new Bundle();

                    bundle.putString("oid", s);

                    startActivity(new Intent(HomeActivity.this, ConfirmOrderActivity.class).putExtras(bundle));

                    break;

            }

            MyToast.showToast(toast, context);


        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }
    });

    //修改购物车
    private UpdateShopCarPost updateShopCarPost = new UpdateShopCarPost(new AsyCallBack<String>() {

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            Log.e("---", "type" + type);

            if (type == 1) {

                shopCarLists.get(addOrMinusPos).num  = (Integer.parseInt(shopCarLists.get(addOrMinusPos).num) + 1) + "";

                shopCarAdapter.notifyDataSetChanged();


            } else if (type == 2) {

                shopCarLists.get(addOrMinusPos).num = (Integer.parseInt(shopCarLists.get(addOrMinusPos).num) - 1) + "";

                if (shopCarLists.get(addOrMinusPos).num.equals("0")) {//当是0个的时候就把数据清空了

                   shopCarLists.remove(addOrMinusPos);

                }

                shopCarAdapter.notifyDataSetChanged();

            } else if (type == 0) {

                MyToast.showToast("成功加入购物车", context);

                isAdd = true;

//                startMyAnimation(home_shopcar_add_show_tv);

            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }


        @Override
        public void onEnd(String toast, int type) throws Exception {

            isClick = true;
        }

    });

    //购物车展示
    private ShopCarShowGet shopCarShowGet = new ShopCarShowGet(new AsyCallBack<ShopCarShowGet.ShopCarShowEntity>() {

        @Override
        public void onSuccess(String toast, int type, ShopCarShowGet.ShopCarShowEntity shopCarShowEntity) throws Exception {

            shopCarLists.addAll(shopCarShowEntity.shopCarEntityList);

            shopCarAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }


    });

    //商品详情 展示  有轮播图  还有文字
    private GoodsDetailGet goodsDetailGet = new GoodsDetailGet(new AsyCallBack<GoodsDetailGet.GoodsDetailEntity>() {

        @Override
        public void onSuccess(String toast, int type, GoodsDetailGet.GoodsDetailEntity goodsDetailEntity) throws Exception {

            goodsDetail = goodsDetailEntity;

            home_right_title_tv.setText(goodsDetailEntity.title);
            home_right_price_tv.setText(goodsDetailEntity.deprice);
            home_right_title_two_tv.setText(goodsDetailEntity.letter);
            home_right_content_tv.setText(goodsDetailEntity.thumb);

            home_right_bottom_title_tv.setText(goodsDetailEntity.title);

            convenientBanner.setPages(
                    new CBViewHolderCreator<LocalImageHolderView>() {
                        @Override
                        public LocalImageHolderView createHolder() {
                            return new LocalImageHolderView();
                        }
                    }, goodsDetailEntity.picArr).setPageIndicator(new int[]{R.drawable.banner_no_sel_bg, R.drawable.banner_sel_bg})
                    //设置指示器的方向
                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_LEFT)
                    .startTurning(5000)
                    .setCanLoop(true);

            //view显示时的动画
            MyAnimationUitls.toVisibleAnim(home_right_scroll_sv);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }


    });

    //商品类型列表
    private GoodsTypeGet goodsTypeGet = new GoodsTypeGet(new AsyCallBack<GoodsTypeGet.GoodsTypeEntity>() {

        @Override
        public void onSuccess(String toast, int type, GoodsTypeGet.GoodsTypeEntity goodsTypeEntity) throws Exception {

            tabLists.addAll(goodsTypeEntity.goodsTypeList);

            homeTabAdapter.notifyDataSetChanged();

            home_indicator.setTabItemTitles(tabLists);

            home_indicator.highLightTextView(0);

            goodsLists.clear();

            //显示tab第一个的列表
            goodsListGet.tid = tabLists.get(0).id;
            goodsListGet.sid = BaseApplication.BasePreferences.readShopId();
            goodsListGet.title = "";
            goodsListGet.execute(HomeActivity.this, false);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }
    });

    //商品列表
    private GoodsListGet goodsListGet = new GoodsListGet(new AsyCallBack<GoodsListGet.GoodsListEntity>() {

        @Override
        public void onSuccess(String toast, int type, GoodsListGet.GoodsListEntity goodsListEntity) throws Exception {

            goodsLists.addAll(goodsListEntity.goodsListEntity);

            home_list_no_show_tv.setVisibility(View.GONE);

            if (goodsLists.size() > 0) {

                showSelPosition = 0;
                homeShowAdapter.showSelPosition = 0;

                // 商品详情
                goodsDetailGet.gid = goodsLists.get(0).id;
                goodsDetailGet.execute(context, false);

                MyAnimationUitls.toVisibleAnim(home_show_lv);

            } else {

                home_list_no_show_tv.setVisibility(View.VISIBLE);

            }

            homeShowAdapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);

            home_list_no_show_tv.setVisibility(View.VISIBLE);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            home_search_search_iv.setEnabled(true);

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        clearnShopCarPost.sid = BaseApplication.BasePreferences.readShopId();
        clearnShopCarPost.uid = BaseApplication.BasePreferences.readUserId();
        clearnShopCarPost.execute(this, false);

        // 左上横向listview 菜的种类 点击事件
        home_tab_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (tabSelPosition != position) {

                    homeTabAdapter.selPosition = position;
                    homeTabAdapter.notifyDataSetChanged();

                    goodsLists.clear();

                    home_show_lv.setVisibility(View.GONE);

                    //按type 显示商品列表
                    goodsListGet.tid = tabLists.get(position).id;
                    goodsListGet.sid = BaseApplication.BasePreferences.readShopId();
                    goodsListGet.title = "";
                    goodsListGet.execute(HomeActivity.this, false);

                    tabSelPosition = position;

                    //接口成功了才让显示
                    home_right_scroll_sv.setVisibility(View.GONE);
                }

            }
        });

        home_tab_lv.setAdapter(homeTabAdapter = new HomeTabAdapter(context, tabLists));

        // 左边的listview 展示菜用的 的点击事件
        home_show_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (showSelPosition != position) {

                    homeShowAdapter.showSelPosition = position;
                    homeShowAdapter.notifyDataSetChanged();

                    //按商品列表id 显示商品详情
                    goodsDetailGet.gid = goodsLists.get(position).id;
                    goodsDetailGet.execute(context, false);

                    showSelPosition = position;

                    //接口成功了才让显示
                    home_right_scroll_sv.setVisibility(View.GONE);

                }

            }
        });

        home_show_lv.setAdapter(homeShowAdapter = new HomeShowAdapter(context, goodsLists));

        goodsTypeGet.sid = BaseApplication.BasePreferences.readShopId();
        goodsTypeGet.execute(this);

        setAppCallBack(new RefreshCount());

        //设置别名
        if (!BaseApplication.BasePreferences.readIsSetTag()) {

            SetTagUtils setTagUtils = new SetTagUtils();

            setTagUtils.setTags(context);
        }

        //购物车的popupWindow
        initPopupWindow();

        // 下面是手势监听，还有点击监听
        home_right_scroll_sv.setOnTouchListener(this);

        //下面两个要记得设哦，不然就没法处理轻触以外的事件了，例如抛掷动作。

        home_right_scroll_sv.setLongClickable(true);

        detector = new GestureDetector(this);

        detector.setIsLongpressEnabled(true);


        home_search_iv.setOnClickListener(this);
        home_search_search_iv.setOnClickListener(this);
        home_search_click_gone_iv.setOnClickListener(this);
        home_business_center_iv.setOnClickListener(this);
        home_shop_car_iv.setOnClickListener(this);
        home_add_to_shopcar_tv.setOnClickListener(this);
        home_tab_iv.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.home_tab_iv:

                if (tabSelPosition < tabLists.size() - 1) {

                    if (tabSelPosition == -1) {

                        home_horizontal_sv.smoothScrollTo(0, 0);

                    } else {

                        home_horizontal_sv.smoothScrollTo(home_indicator.moveX.get(tabSelPosition), 0);

                    }


                    home_indicator.resetTextViewColor(tabSelPosition);
                    home_indicator.highLightTextView(tabSelPosition + 1);

                    goodsLists.clear();

                    //按type 显示商品列表
                    goodsListGet.tid = tabLists.get(tabSelPosition + 1).id;
                    goodsListGet.sid = BaseApplication.BasePreferences.readShopId();
                    goodsListGet.title = "";
                    goodsListGet.execute(HomeActivity.this, false);

                    tabSelPosition = tabSelPosition + 1;

                    //接口成功了才让显示
                    home_right_scroll_sv.setVisibility(View.GONE);


                }

                break;

            case R.id.home_add_to_shopcar_tv:

                updateShopCarPost.uid = BaseApplication.BasePreferences.readUserId();
                updateShopCarPost.sid = BaseApplication.BasePreferences.readShopId();
                updateShopCarPost.gid = goodsDetail.id;
                updateShopCarPost.type = "1";
                updateShopCarPost.cartsn = "";
                updateShopCarPost.oid = orderId;

                updateShopCarPost.execute(this);

                break;

            case R.id.home_search_iv:

                home_search_gone_rl.setVisibility(View.VISIBLE);
                home_search_iv.setVisibility(View.GONE);

                break;

            case R.id.home_search_click_gone_iv:

                home_search_gone_rl.setVisibility(View.GONE);
                home_search_iv.setVisibility(View.VISIBLE);

                break;

            case R.id.home_search_search_iv:

                String searchContent = home_search_content_et.getText().toString().replaceAll(" ", "");

                if (TextUtils.isEmpty(searchContent)) {

                    MyToast.showToast("请输入搜索内容", context);
                    return;

                }

                //下面三行是  让 tab 选中取消 回到最左边
                home_horizontal_sv.smoothScrollTo(0, 0);

                home_indicator.resetTextViewColor(tabSelPosition);

                tabSelPosition = -1;

                home_search_search_iv.setEnabled(false);

                goodsLists.clear();

                //按type 显示商品列表
                goodsListGet.tid = "";
                goodsListGet.title = searchContent;
                goodsListGet.sid = BaseApplication.BasePreferences.readShopId();
                goodsListGet.execute(HomeActivity.this, true);

                //接口成功了才让显示
                home_right_scroll_sv.setVisibility(View.GONE);

                break;

            case R.id.home_business_center_iv:

                cleanShopCar();

                startActivity(new Intent(this, BusinessCenterLoginActivity.class));

                break;

            case R.id.home_shop_car_iv:

                shopCarLists.clear();

                shopCarShowGet.sid = BaseApplication.BasePreferences.readShopId();
                shopCarShowGet.uid = BaseApplication.BasePreferences.readUserId();
                shopCarShowGet.execute(HomeActivity.this, false);

                mPopupWindow.showAtLocation(home_ll, Gravity.RIGHT, 0, 0);

                break;

            case R.id.shop_car_arrow_rl:

                mPopupWindow.dismiss();

                break;

            case R.id.item_shop_car_pay_tv: //popupwindow 的支付按钮

                if (shopCarLists.size() > 0) {

                    submitOrderPost.sid = BaseApplication.BasePreferences.readShopId();
                    submitOrderPost.uid = BaseApplication.BasePreferences.readUserId();
                    submitOrderPost.seats = item_shop_table_num_tv.getText().toString().trim();
                    submitOrderPost.oid = orderId;

                    submitOrderPost.execute(this, 1);


                } else {

                    MyToast.showToast("请选择商品", context);
                }

                mPopupWindow.dismiss();

                break;

            case R.id.item_shop_car_submit_order_tv://popupwindow 的提交订单按钮

                Log.e("----", "orderId" + orderId + "    isFromOrder" + isFromOrder);

//                if (TextUtils.isEmpty(item_shop_table_num_tv.getText().toString().trim()) && !isFromOrder) {
//
//                    MyToast.showToast("请输入桌号", context);
//                    return;
//                }

                submitOrderPost.sid = BaseApplication.BasePreferences.readShopId();
                submitOrderPost.uid = BaseApplication.BasePreferences.readUserId();
                submitOrderPost.seats = item_shop_table_num_tv.getText().toString().trim();
                submitOrderPost.oid = orderId;

                submitOrderPost.execute(this);

                break;

        }

    }

    private void cleanShopCar() {

        if (shopCarLists.size() > 0 || isAdd) {

            clearnShopCarPost.sid = BaseApplication.BasePreferences.readShopId();
            clearnShopCarPost.uid = BaseApplication.BasePreferences.readUserId();
            clearnShopCarPost.execute(this, false);

        }

        isAdd = false;

        //下单成功后就把这两个值设置，继续点单用的值，避免下次再继续点单
        orderId = "";
        isFromOrder = false;

        //桌号清空
        item_shop_table_num_tv.setText("");
        item_shop_table_num_tv.setEnabled(true);
        item_shop_table_num_tv.setHint("餐后付款请输入");

        mPopupWindow.dismiss();

    }


    //回调点击事件
    @Override
    public void add(int pos) {

        if (isClick) {

            addOrMinusPos = pos;

            isClick = false;

            updateShopCarPost.uid = BaseApplication.BasePreferences.readUserId();
            updateShopCarPost.sid = BaseApplication.BasePreferences.readShopId();
            updateShopCarPost.gid = shopCarLists.get(pos).gid;
            updateShopCarPost.type = "1";
            updateShopCarPost.cartsn = "";
            updateShopCarPost.oid = orderId;

            updateShopCarPost.execute(this, false, 1);
        }
    }

    @Override
    public void minus(int pos) {

        if (isClick) {

            addOrMinusPos = pos;

            isClick = false;

            updateShopCarPost.uid = BaseApplication.BasePreferences.readUserId();
            updateShopCarPost.sid = BaseApplication.BasePreferences.readShopId();
            updateShopCarPost.gid = shopCarLists.get(pos).gid;
            updateShopCarPost.type = "-1";
            updateShopCarPost.cartsn = "";
            updateShopCarPost.oid = orderId;

            updateShopCarPost.execute(this, false, 2);

        }

    }


    //dialog 回调事件 去支付
    @Override
    public void pay(int pos) {

        cleanShopCar();

        if (notPayDialog != null && notPayDialog.isShowing()) {

            Bundle bundle = new Bundle();

            bundle.putString("oid", notPayDialog.lists.get(pos).id);

            startActivity(new Intent(this, ConfirmOrderActivity.class).putExtras(bundle));

            notPayDialog.dismiss();

        }


    }

    //dialog 回调事件 继续点菜
    @Override
    public void continueChoose(int pos) {

        cleanShopCar();

        if (notPayDialog != null && notPayDialog.isShowing()) {

            Bundle bundle = new Bundle();

            bundle.putString("seats", notPayDialog.lists.get(pos).seats);
            bundle.putString("oid", notPayDialog.lists.get(pos).id);
            bundle.putBoolean("isFromOrder", true);

            startActivity(new Intent(this, HomeActivity.class).putExtras(bundle));

            notPayDialog.dismiss();

        }

    }

    //notpayDialog item点击事件回调
    @Override
    public void notPayOnItemclick(int pos) {

        startActivity(new Intent(this, OrderDetailActivity.class).putExtra("orderId", notPayDialog.lists.get(pos).id));

        notPayDialog.dismiss();

    }

    //手势的监听
    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        try {

//            if (e1.getY() - e2.getY() < 50 || e2.getY() - e1.getY() > 50) {
//
//                return false;
//            }

            if (e1.getX() - e2.getX() > ScaleScreenHelperFactory.getInstance().getWidthHeight(150) ) {

    //            updateShopCarPost.uid = BaseApplication.BasePreferences.readUserId();
    //            updateShopCarPost.sid = BaseApplication.BasePreferences.readShopId();
    //            updateShopCarPost.gid = goodsDetail.id;
    //            updateShopCarPost.sid = BaseApplication.BasePreferences.readShopId();
    //            updateShopCarPost.type = "-1";
    //            updateShopCarPost.cartsn = "";
    //            updateShopCarPost.oid = orderId;
    //
    //            updateShopCarPost.execute(this, true, 1);

                notPayDialog = new NotPayDialog(context, this);

                notPayDialog.show();


            } else if (e2.getX() - e1.getX() > ScaleScreenHelperFactory.getInstance().getWidthHeight(150) ) {

                updateShopCarPost.uid = BaseApplication.BasePreferences.readUserId();
                updateShopCarPost.sid = BaseApplication.BasePreferences.readShopId();
                updateShopCarPost.gid = goodsDetail.id;
                updateShopCarPost.type = "1";
                updateShopCarPost.cartsn = "";
                updateShopCarPost.oid = orderId;

                updateShopCarPost.execute(this);


            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        detector.onTouchEvent(event);

        return false;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);

        // 继续点单
        Bundle bundle = intent.getExtras();

        // 继续点单 用orderId 首次不传
        String seats = bundle.getString("seats", "");
        orderId = bundle.getString("oid", "");
        isFromOrder = bundle.getBoolean("isFromOrder", false);

        if (isFromOrder) {

            item_shop_table_num_tv.setEnabled(false);

            if(TextUtils.isEmpty(seats)) {

                item_shop_table_num_tv.setHint("继续点单不输入");

            } else {

                item_shop_table_num_tv.setText(seats);

            }

        }

    }


    public void initPopupWindow() {

        View popupView = BoundViewHelper.boundView(this, ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) getLayoutInflater().inflate(R.layout.item_shop_car, null)));

        RelativeLayout shop_car_arrow_rl = (RelativeLayout) popupView.findViewById(R.id.shop_car_arrow_rl);
        TextView item_shop_car_pay_tv = (TextView) popupView.findViewById(R.id.item_shop_car_pay_tv);
        TextView item_shop_car_submit_order_tv = (TextView) popupView.findViewById(R.id.item_shop_car_submit_order_tv);
        item_shop_table_num_tv = (TextView) popupView.findViewById(R.id.item_shop_table_num_tv);

        shop_car_arrow_rl.setOnClickListener(this);
        item_shop_car_pay_tv.setOnClickListener(this);
        item_shop_car_submit_order_tv.setOnClickListener(this);

        ListView popList = (ListView) popupView.findViewById(R.id.item_shop_car_pop_lv);

        popList.setDivider(null);

        popList.setVerticalScrollBarEnabled(false);

        shopCarAdapter = new ShopCarAdapter(context, shopCarLists, HomeActivity.this);

        popList.setAdapter(shopCarAdapter);

        mPopupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);

        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());

        mPopupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    }

    //Linearlayout 点击事件
    @Override
    public void onclick(int pos) {

        if (tabSelPosition != pos) {

//            homeTabAdapter.selPosition = pos;
//            homeTabAdapter.notifyDataSetChanged();

            //接口成功了才让显示
            home_right_scroll_sv.setVisibility(View.GONE);

            home_indicator.resetTextViewColor(tabSelPosition);
            home_indicator.highLightTextView(pos);

            goodsLists.clear();

            //按type 显示商品列表
            goodsListGet.tid = tabLists.get(pos).id;
            goodsListGet.sid = BaseApplication.BasePreferences.readShopId();
            goodsListGet.title = "";
            goodsListGet.execute(HomeActivity.this, false);

            tabSelPosition = pos;

        }
    }

    class LocalImageHolderView implements Holder<String> {

        private ImageView imageView;

        @Override
        public View createView(Context context) {

            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, String data) {

            GlideLoader.getInstance().get(context, data, imageView, R.mipmap.banner_holder);

        }

    }

    public void startMyAnimation (final View view) {

        view.setVisibility(View.VISIBLE);

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.shopcar_add_anim);

        view.startAnimation(animation);

        animation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
                view.setVisibility(View.GONE);

            }
        });





//        private AnimatorSet animSet = new AnimatorSet();
//
//        float vy = view.getY();
//
//        ObjectAnimator anim0 = ObjectAnimator.ofFloat(view, "alpha", 0f, 1.0f);
//
//        ObjectAnimator anim1 = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 2f);
//
//        ObjectAnimator anim2 = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 2f);
//
//        ObjectAnimator anim3 = ObjectAnimator.ofFloat(view, view.Y, vy, vy - 110);
//
//        ObjectAnimator anim4 = ObjectAnimator.ofFloat(view, "alpha", 1.0f, 0f);
//
//        ObjectAnimator anim5 = ObjectAnimator.ofFloat(view, view.Y, vy);
//
//        if (animSet.isRunning()) {
//
//            Log.e("animset", "animtset Is Running");
//            animSet.cancel();
//        }
//
//        animSet.play(anim0).with(anim1);
//        animSet.play(anim1).with(anim2);
//        animSet.play(anim2).with(anim3);
//        animSet.play(anim4).after(anim3);
//        animSet.play(anim5).after(anim4);
//        animSet.setDuration(1000);
//        animSet.start();


    }

    public class RefreshCount implements AppCallBack {

        void refresh() {

            goodsLists.clear();
            //下单成功走接口，为了显示卖出多少个 刷新一下数据
            goodsListGet.execute(HomeActivity.this, false);
        }
    }


}
